package com.fingard.dsp.task.command;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import com.fingard.FGBiz;
import com.fingard.dsp.bank.dbobj.receipt.ReceiptData;
import com.fingard.dsp.bank.dbobj.receipt.ownact.OwnActData;
import com.fingard.dsp.bank.dbobj.receipt.ownact.OwnActHelper;
import com.fingard.dsp.bank.directConfig.ReceiptProConfig;
import com.fingard.dsp.task.ITask;
import com.fingard.dsp.task.TaskBase;
import com.fingard.dsp.task.command.receipt.PushReceiptData;
import com.fingard.dsp.task.command.receipt.UploadReceiptFile;
import com.fingard.text.StringHelper;


/**同步回单*/
public class SyncReceipt extends TaskBase implements ITask {

	protected ReceiptProConfig proConfig = null;
	protected String logCode = "task"+File.separator+"SyncReceipt";
	
	public static void resetDelayCounter(){
		UploadReceiptFile.resetDelayCounter();
		PushReceiptData.resetDelayCounter();
	}
	
	private static long polledCount = 0;//轮询次数
	public long getPolledCount(){
		return polledCount;
	}
	
	private static Date lastPolledTime;//最近轮询时间
	public Date getLastPolledTime(){
		return lastPolledTime;
	}
	
	/**pParams的key已转小写
	 *          <paramList>
                    <bank></bank>
                    <beginDayCount>30</beginDayCount>
                    <endDayCount>0</endDayCount>
                </paramList>
           */
	@Override
	public void execTask(HashMap<String, String> pParams) {
		lastPolledTime = new Date();
		polledCount++;
		try{
			//回单文件上传
			UploadReceiptFile upTask = new UploadReceiptFile(logCode);
			upTask.logID = this.logID;
			upTask.taskName = this.taskName;
			upTask.execTask(pParams);
			
			//回单数据推送
			PushReceiptData pushTask = new PushReceiptData(logCode);
			pushTask.logID = this.logID;
			pushTask.taskName = this.taskName;
			pushTask.execTask(pParams);
    		
		}catch(Throwable t){
			FGBiz.logTrace.exception(logCode, t);
		}finally{
			
		}
	}
	
	/**取int参数*/
	protected int getParamAsInt(HashMap<String, String> pParams, String pKey, int pDefVal){
		if(pParams.containsKey(pKey)){
			String beginDay = pParams.get(pKey);
			if(StringHelper.hasAnyChar(beginDay)){
				return Integer.parseInt(beginDay);
			}
		}
		return pDefVal;
	}
	
	/**取参数：开始天数和结束天数*/
	protected int[] getBeginEndDay(HashMap<String, String> pParams){
		int[] retVal = new int[2];
		retVal[0] = getParamAsInt(pParams, "beginDayCount".toLowerCase(), -2);
		retVal[1] = getParamAsInt(pParams, "endDayCount".toLowerCase(), 0);
		if(retVal[0] > retVal[1]){
			FGBiz.logTrace.info(logCode, "开始天数大于结束天数，两者互换");
			int chg = retVal[0];
			retVal[0] = retVal[1];
			retVal[1] = chg;
		}
		if(retVal[1] > 0){
			FGBiz.logTrace.info(logCode, "结束天数"+retVal[1]+"不应该大于0");
		}
		return retVal;
	}
	
	/**格式化文件路径
	 * @throws Exception */
	protected String replaceDestFilePathFormat(String pSubPathFormat, ReceiptData pData) throws Exception{
		String retPath = replaceDestDirFormat(pSubPathFormat, pData);
		String ext = StringHelper.getFileExt(pData.localFile);
		boolean hasName = false;
		if(retPath.contains("原始回单文件名")){
			hasName = true;
			String fileName = StringHelper.getFileName(pData.srcFile);
			if(StringHelper.isNullOrEmpty(fileName)){
				fileName = StringHelper.getFileName(pData.localFile);
			}
			retPath = retPath.replace("原始回单文件名", fileName);
		}
		if(retPath.contains("回单文件名")){
			hasName = true;
			String fileName = pData.receiptNo+"."+ext;
			retPath = retPath.replace("回单文件名", fileName);
		}

		if(!hasName){
			retPath = StringHelper.trimEndFileSp(retPath)+File.separator+pData.receiptNo+"."+ext;
		}
		return retPath;
	}
	
	/**格式化目录*/
	protected String replaceDestDirFormat(String pSubPath, ReceiptData pData) throws Exception{
		//银行字母代码
		String bankEn = proConfig.getMapValueByKey("bankEnMapping", pData.bank);
		if(StringHelper.isNullOrEmpty(bankEn)){
			if("102".equals(pData.bank)){
				bankEn = "ICBC";
			}else if("103".equals(pData.bank)){
				bankEn = "ABC";
			}else if("104".equals(pData.bank)){
				bankEn = "BOC";
			}else if("105".equals(pData.bank)){
				bankEn = "CCB";
			}else{
				bankEn = "OTHER";
			}
		}
		
		String year = pData.getBusYear();
		String month = pData.getBusMonth();
		String day = pData.getBusDay();
		//----/银行字母代码/年月/账号
		String subPath = pSubPath.replace("银行字母代码", bankEn).replace("年", year).replace("月", month).replace("日", day).replace("账号", pData.ownAct);
		if(subPath.contains("按回单编号拆分目录")){
			String spByRcpt = StringHelper.getDirPathUsingSplitGiveUpLastOne(pData.receiptNo, 3);
			subPath = subPath.replace("按回单编号拆分目录", spByRcpt);
		}
		if(subPath.contains("主管组织")){
			OwnActHelper ownHelper = new OwnActHelper();
			OwnActData ownData = ownHelper.loadOwnActData(pData.ownAct);
			String cmptOrg = proConfig.getMapValueByKey("cmptOrgMapping", ownData.cmptOrg);
			if(StringHelper.isNullOrEmpty(cmptOrg) && StringHelper.hasAnyChar(ownData.cmptOrg)){
				cmptOrg = ownData.cmptOrg;
			}
			subPath = subPath.replace("主管组织", cmptOrg);
		}
		return subPath;
	}
	
	protected String getFileType(String pPath){
		String ext = StringHelper.getFileExt(pPath);
		if("pdf".equalsIgnoreCase(ext)){
			return "1";
		}else if("jpg".equalsIgnoreCase(ext)||"jpeg".equalsIgnoreCase(ext)||"png".equalsIgnoreCase(ext)){
			return "2";
		}else{
			return "";
		}
	}

}
